Assignment statements set the value of a variable or like identifier in a script. Assignment statements use the assignment operator (
:=) to set the value of the identifier on the left-hand side of the symbol to the value of the constant or identifier on the right-hand side of the symbol. This may also be thought of as assigning the value of the identifier on the right-hand side to the identifier on the left.
From the example, it is evident that the assignment statement is very flexible. The example makes use of constants, variables, structure fields, and function return values when assigning values to an identifier. Note also that more than one statement can reside on a single line, as long as they are separated by a semi-colon indicating the end of each statement.
Assignment statements also support block copying of values in arrays when they are used without an array element index in a script. This method facilitates transferring large amounts of data without the need for copying on an element-by-element basis. For example:
In order to transfer the values in values1 to
values2, it would appear that multiple assignment statements are needed, one for each array element. For large arrays, this would be a time-consuming task. Fortunately, VectorScript overloads (extends the functionality of) the assignment operator so that operation to copy the values becomes a single statement:
The assignment statement copies the data from the values1 array directly into the corresponding elements of the
values2 array. This sort of assignment operation can be also be performed with dynamic arrays; in both cases, however, the dimensions of the arrays on both sides of the assignment operator must be exactly the same in order to complete the operation.
Vectors and structures may also be copied in this manner; the member values of the item on the right side of the assignment operator will be copied into the corresponding member fields of the item on the left side of the operator. For example, the values in a vector
direction_vector1 could be copied into another vector:
The values in the fields of direction_vector1 would be copied into the fields of
new_vector without the need for assignment statements for each field.